package com.stone.oauth.client.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>Created with IntelliJ IDEA. </p>
 * <p>User: Stony </p>
 * <p>Date: 2016/7/7 </p>
 * <p>Time: 15:33 </p>
 * <p>Version: 1.0 </p>
 */
public class LoginFilter implements Filter {
    private static final Logger logger = LoggerFactory.getLogger(LoginFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("Enter");
    }

    /**
     * 远程授权登陆
     * 回掉地址 http://localhost:8081/auth
     * 授权验证 client_id=454545454&response_type=code&redirect_uri=http://xxx.com/path
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        logger.info("Enter");
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.sendRedirect("http://localhost:8080/authorize?client_id="+AuthFilter.clientId+"&response_type=code&redirect_uri=http://localhost:8081/auth");
        return;
    }

    @Override
    public void destroy() {
        logger.info("Enter");
    }
}
